package com.bruip.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bruip.reggie.common.BaseResponse;
import com.bruip.reggie.dto.SetmealDto;
import com.bruip.reggie.pojo.Category;
import com.bruip.reggie.pojo.Setmeal;
import com.bruip.reggie.service.CategoryService;
import com.bruip.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 套餐业务
 *
 * @author 王若龙
 * @date 2022-10-06 19:01
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 套餐分页查询
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param name     名字
     * @return {@link BaseResponse}<{@link Page}>
     */
    @GetMapping("/page")
    public BaseResponse<Page> getPage(int page, int pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        queryWrapper.orderByAsc(Setmeal::getCategoryId);
        setmealService.page(setmealPage, queryWrapper);
        /*
           以下代码
           给菜品列表中的类别名称赋值
         */
        //对象拷贝
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");

        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealDto> list = records.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);

        return BaseResponse.success(setmealDtoPage);
    }

    /**
     * 新增套餐信息的保存
     *
     * @param setmealDto setmeal dto
     * @return {@link BaseResponse}<{@link String}>
     */
    @PostMapping
    @CacheEvict(value = "setMealCache", allEntries = true)
    public BaseResponse<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return BaseResponse.success("新增套餐成功");
    }

    @GetMapping("/{id}")
    public BaseResponse<SetmealDto> updatePage(@PathVariable("id") Long id) {
        SetmealDto setmeal = setmealService.getInfoById(id);
        return BaseResponse.success(setmeal);
    }

    @CacheEvict(value = "setMealCache", allEntries = true)
    @PutMapping
    public BaseResponse<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.udateSetMeal(setmealDto);
        return BaseResponse.success("套餐修改成功");
    }

    @CacheEvict(value = "setMealCache", allEntries = true)
    @DeleteMapping
    public BaseResponse<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return BaseResponse.success("套餐删除成功");
    }

    /**
     * 设置状态
     *
     * @param ids    id
     * @param status 状态
     * @return {@link BaseResponse}<{@link String}>
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setMealCache", allEntries = true)
    public BaseResponse<String> setStatus(@RequestParam List<Long> ids, @PathVariable("status") Integer status) {
        LambdaUpdateWrapper<Setmeal> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.set(Setmeal::getStatus, status);
        setmealService.update(queryWrapper);
        return BaseResponse.success("状态修改成功");
    }

    @CachePut(value = "setMealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    @GetMapping("/list")
    public BaseResponse<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return BaseResponse.success(list);
    }
}
